package com.digitaldreamers.framework.network.packet;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;

/**
 * Abstract class for all sendable packet classes.
 *
 * @author DaoLe
 */
public abstract class AbSendablePacket {

    // Packet ID.
    protected int mPacketId;

    /**
     * Convert packet data to byte array.
     *
     * @return The converted byte array.
     * @throws IOException Error that occurred while converting data.
     */
    public byte[] toByteArray() throws IOException {
    	ByteArrayOutputStream BAOS = new ByteArrayOutputStream();
        DataOutputStream DOS = new DataOutputStream(BAOS);
        
        writeData(DOS);
        byte[] data = BAOS.toByteArray();
        
        DOS.close();
        BAOS = null;
        DOS = null;
        return data;
    }
    
    /**
     * Write packet data.
     * 
     * @param DOS Output stream
     * @throws IOException Error that occurred while writing data.
     */
    protected abstract void writeData(DataOutputStream pDOS) throws IOException;
}